package com.eric.downloader;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

public class ThreeSixtyRecDownloader extends SimplePageDownloader{
	
	String queryBaseUrl = "http://www.so.com/s?src=opensearch&q=";
	
	String kw;
	
	public static void main(String[] args) {
//		MyProxy p = new MyProxy();
		ThreeSixtyRecDownloader bdr = new ThreeSixtyRecDownloader("美妆");
		
		System.out.println(bdr.analyze());
	}
	
	public ThreeSixtyRecDownloader (String kw, MyProxy p) {
		super(p);
		this.kw = kw;
		this.p = p;
	}
	
	public ThreeSixtyRecDownloader (String kw) {
		this.kw = kw;
	}
	
	public JSONObject analyze(String kw) {
		this.kw = kw;
		return analyze();
	}

	public JSONObject analyze() {
		String s = null;
		try {
			s = URLEncoder.encode(kw,"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} 
		
		querySearchEngine(queryBaseUrl,s);
		
		JSONObject json = new JSONObject();
		List<String> list = new ArrayList<>();
		String[] a = doc.getElementsByAttributeValue("id", "rs").select("a").text().split(" ");
		for (int i=0;i<a.length;i++) {
			list.add(a[i]);
		}
		json.put("kw", kw);
		json.put("rec", list);
		return json;
	}

}
